<?php
/**
 * User: Peter Wang
 * Date: 17/3/16
 * Time: 下午12:19
 */

namespace App\Lib\Support;

use Trensy\Component\Rpc\RpcClient;
class Rpc
{
        protected $client = null;
        protected $result = null;
        protected $error = null;
        protected $statusCode = null;
        protected $elapsedTime = 0;
        protected $check = false;
        protected $all = null;

        protected static $clientInstance = null;

        public static function client()
        {
            return new self();
        }


        public function __construct()
        {
            $config = config()->get('client.rpc');
        
            $client = new RpcClient($config['host'], $config['port'], $config['serialization'], $config);

            $this->client = $client;
        }


        public function get($path, $params)
        {
            $result = $this->client->get($path, $params);
            $this->all = $result;
            $this->statusCode = $result['statusCode'];
            $this->elapsedTime = $result['elapsedTime'];

            if($result['statusCode'] == 200){
                $this->result = $result['result'];
                $this->check = true;
                return $this;
            }
            $this->error = $result['msg'];
            $this->check = false;
            return $this;
        }

        public function check()
        {
            return $this->check;
        }


        public function getError()
        {
            return $this->error;
        }

        public function getStatusCode()
        {
            return $this->statusCode;
        }

        public function getResult()
        {
            return $this->result;
        }

        public function getElapsedTime()
        {
            return $this->elapsedTime;
        }

        public function getAll()
        {
            return $this->all;
        }
}